﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;

namespace CoolERP_Web.User
{
    public partial class CGD_Query :BasePage
    {
        protected string DJ_Name;//单据类型
        protected string MX_URL;//明细页面URL
        protected string Edit_URL;//新增、修改页面URL
        //private int DJ_Type
        //{
        //    get { return Convert.ToInt32(this.GetUrlParam("lx")); }
        //}
        private int DjStatus
        {
            get { return this.ViewState["DjStatus"] == null ? 0 : Convert.ToInt32(this.ViewState["DjStatus"]); }
            set { this.ViewState["DjStatus"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFirst();
                BindData();
            }
        }

        private void BindFirst()
        {
            //if (DJ_Type == 0)
            //{
            //    DJ_Name = "物料";
            //    MX_URL = "JCCDMX_JC_WL.aspx";
            //    Edit_URL = "JCCD_EditJC_WL.aspx";
            //}
            //else
            //{
            //    DJ_Name = "包装箱";
            //    MX_URL = "JCCDMX_JC_BZX.aspx";
            //    Edit_URL = "JCCD_EditJC_BZX.aspx";
            //}
            MX_URL = "CGDMX.aspx";
            Edit_URL = "CGD_Edit.aspx";
            imgAdd.PostBackUrl = Edit_URL;
            this.AspNetPager1.PageSize = GridView1.PageSize;
            //BindDepartment(this.dd, "请选择");
            BindGYS(this.ddlGYS, "请选择");
            BindDepartment(this.ddlBMID, "请选择");
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        protected void LinkButton_Command(object sender, CommandEventArgs e)
        {
            DjStatus = Convert.ToInt32(e.CommandArgument);
            this.AspNetPager1.CurrentPageIndex = 1;
            GridView1.Columns[7].Visible = DjStatus == 0;
            BindData();
        }

        private void BindData()
        {
            Dictionary<string, string> dc = new Dictionary<string, string>();
            //if (DJ_Type == 0)
            //    dc.Add("YWLX", EnumMgr<EnumYWDJLX>.GetEnumValue(EnumYWDJLX.WL_JC));
            //else
            //    dc.Add("YWLX", EnumMgr<EnumYWDJLX>.GetEnumValue(EnumYWDJLX.BZX_JC));
            dc.Add("ZT", this.DjStatus.ToString());
            if (!string.IsNullOrEmpty(tbBH.Text.Trim()))
                dc.Add("BH", tbBH.Text.Trim());
            if (!string.IsNullOrEmpty(ddlBMID.SelectedValue))
                dc.Add("BMID", ddlBMID.SelectedValue);
            if (!string.IsNullOrEmpty(ddlGYS.SelectedValue))
                dc.Add("GYSID", ddlGYS.SelectedValue);
            if (!string.IsNullOrEmpty(txtCJSJ.Text.Trim()))
                dc.Add("CJSJ", txtCJSJ.Text.Trim());
            if (!string.IsNullOrEmpty(txtRQ.Text.Trim()))
                dc.Add("RQ", txtRQ.Text.Trim());
            if (!string.IsNullOrEmpty(txtDHSJ.Text.Trim()))
                dc.Add("DHSJ", txtDHSJ.Text.Trim());
            if (!string.IsNullOrEmpty(tbRY.Text.Trim()))
            {
                dc.Add("RYName", tbRY.Text.Trim());
            }
            if (!string.IsNullOrEmpty(tbCZRY.Text.Trim()))
            {
                dc.Add("CZRYName", tbCZRY.Text.Trim());
            }
            using (CGDBLL xBLL = new CGDBLL())
            {
                int RecordCount = 0;
                List<CGDModel> itemLists = xBLL.GetCGD(dc, true, "BH asc", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                this.AspNetPager1.RecordCount = RecordCount;
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string ID = DataBinder.Eval(e.Row.DataItem, "ID").ToString();
                string ZT = DataBinder.Eval(e.Row.DataItem, "ZT").ToString();

                ImageButton btnEdit = (ImageButton)(e.Row.FindControl("btnEdit"));
                ImageButton btnConfirm = (ImageButton)(e.Row.FindControl("btnConfirm"));
                ImageButton btnDelete = (ImageButton)(e.Row.FindControl("btnDelete"));

                Label lblZT = ((Label)(e.Row.FindControl("lblZT")));
                if (int.Parse(ZT) == 0)
                {
                    lblZT.Text = "待确认";
                    lblZT.ForeColor = System.Drawing.Color.Red;
                    btnConfirm.Visible = true;
                    btnDelete.Visible = true;
                    btnEdit.Visible = true;
                    btnEdit.PostBackUrl = string.Format("{0}User/{1}?id={2}", AppPath, Edit_URL, ID);
                }
                else
                {
                    lblZT.ForeColor = System.Drawing.Color.Green;
                    lblZT.Text = "已确认";
                    btnConfirm.Visible = false;
                    btnDelete.Visible = false;
                    btnEdit.Visible = false;
                }
            }
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Guid id = Guid.Parse(GridView1.DataKeys[e.NewSelectedIndex].Value.ToString());
            using (CGDBLL xBLL = new CGDBLL())
            {
                if (xBLL.CGDConfirm(id))
                {
                    ShowMessage("确认成功");
                }
                else
                {
                    ShowMessage("确认失败");
                }
            }
            BindData();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            using (CGDBLL xBLL = new CGDBLL())
            {
                CGD del = xBLL.GetEntity(t => t.ID == Guid.Parse(id));
                if (xBLL.Delete(del))
                    ShowMessage("删除成功");
                else
                    ShowMessage("删除失败");
            }
            BindData();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}